CXXFLAGS=-std=c++11 -I. -I../shared -Icppdsaext/src -Wall -Werror -pedantic-errors -fPIC -O2 \
-Wsign-compare -Wpointer-arith -Wswitch-default -Wswitch-enum -Wtype-limits \
-Wmissing-declarations -Wshadow
CXX = @CXX@
LIBS = @LIBS@

# variables set by configure
sbindir = @sbindir@
WITH_DRBDMON=@WITH_DRBDMON@
WITH_WINDRBD = @WITH_WINDRBD@

# variables meant to be overridden from the make command line
ifeq ($(WITH_WINDRBD),yes)
DESTDIR ?=
else
DESTDIR ?= /
endif

binaries := drbdmon

ifeq ($(WITH_DRBDMON),yes)
all: $(binaries)
else
all:
endif

PHONY := all

dsaext-obj := cppdsaext/src/dsaext.o
integerparse-obj := cppdsaext/src/integerparse.o

l-obj := DrbdMon.o DrbdMonConsts.o MessageLog.o IntervalTimer.o SubProcessNotification.o
l-obj += MessageLogNotification.o
l-obj += objects/DrbdResource.o objects/DrbdRole.o objects/DrbdVolume.o objects/DrbdConnection.o
l-obj += objects/VolumesContainer.o objects/StateFlags.o subprocess/EventsSourceSpawner.o
l-obj += objects/ResourceDirectory.o
l-obj += Args.o ConfigOption.o terminal/CharacterTable.o terminal/ColorTable.o terminal/MouseEvent.o
l-obj += terminal/DisplayConsts.o terminal/GlobalCommandConsts.o terminal/ComponentsHub.o terminal/AnsiControl.o
l-obj += terminal/DisplayController.o terminal/DisplayIo.o terminal/DisplayStyleCollection.o
l-obj += terminal/DisplayCommonImpl.o terminal/PosixTermSize.o terminal/SharedData.o
l-obj += terminal/ClickableCommand.o terminal/GlobalCommandsImpl.o terminal/DrbdCommandsImpl.o
l-obj += terminal/EscSeqCodes.o terminal/KeyCodes.o terminal/InputCharCodes.o
l-obj += terminal/MDspMainMenu.o terminal/MDspResources.o terminal/MDspVolumes.o terminal/MDspConnections.o
l-obj += terminal/MDspPeerVolumes.o terminal/MDspLogViewer.o terminal/MDspTaskQueue.o terminal/MDspTaskDetail.o
l-obj += terminal/MDspResourceActions.o terminal/MDspConnectionActions.o terminal/MDspVolumeActions.o
l-obj += terminal/MDspResourceDetail.o terminal/MDspVolumeDetail.o terminal/MDspConnectionDetail.o
l-obj += terminal/MDspBase.o terminal/MDspStdListBase.o terminal/MDspMenuBase.o terminal/MDspMessage.o
l-obj += terminal/InputField.o terminal/DisplayId.o terminal/DisplayIdMap.o terminal/MDspHelp.o
l-obj += terminal/MDspPgmInfo.o terminal/MDspHelpIndex.o terminal/MDspConfiguration.o terminal/MDspWaitMsg.o
l-obj += terminal/TextColumn.o terminal/HelpText.o terminal/DisplayUpdateEvent.o terminal/TerminalControl.o
l-obj += subprocess/SubProcessQueue.o subprocess/SubProcess.o subprocess/CmdLine.o subprocess/DrbdCmdConsts.o
l-obj += subprocess/Linux/SubProcessLx.o terminal/Linux/TerminalControlImpl.o terminal/Linux/InputSequenceDecoder.o
l-obj += platform/Linux/SystemApiImpl.o platform/Linux/EventsIo.o
l-obj += configuration/CfgEntryStore.o configuration/CfgEntryBoolean.o configuration/CfgEntryIntegerTypes.o
l-obj += configuration/CfgEntry.o configuration/Configuration.o configuration/IoException.o
l-obj += persistent_configuration.o
l-obj += StringTokenizer.o comparators.o utils.o exceptions.o integerfmt.o string_transformations.o

ls-obj := drbdmon_main.o $(l-obj) $(dsaext-obj) $(integerparse-obj)

all-obj := $(sort $(ls-obj))
local-obj := $(filter-out $(dsaext-obj) $(integerparse-obj),$(all-obj))
local-dep := $(patsubst %.o,.%.d,$(local-obj))
ifeq ($(WITH_DRBDMON),yes)
# avoid breaking clean target. somebody else has to create that file
ifneq (,$(wildcard ../shared/drbd_buildtag.h))
-include $(local-dep)
endif
endif

$(dsaext-obj): $(basename $(dsaext-obj)).cpp $(basename $(dsaext-obj)).h
$(integerparse-obj): $(basename $(integerparse-obj)).cpp $(basename $(integerparse-obj)).h

drbdmon: $(ls-obj)
	$(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $^ $(LIBS)

# do not try to rebuild Makefile itself
Makefile: ;

.%.d: %.cpp
	@set -e; rm -f $@; \
	$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

install:
ifeq ($(WITH_DRBDMON),yes)
	install -m 755 drbdmon $(DESTDIR)$(sbindir)
endif

uninstall:
ifeq ($(WITH_DRBDMON),yes)
	rm -f $(DESTDIR)$(sbindir)/drbdmon
endif

PHONY += clean distclean
clean:
	rm -f $(local-obj) $(dsaext-obj) $(integerparse-obj) $(binaries)
distclean: clean
	rm -f $(local-dep)

.PHONY: $(PHONY)
